package com.autologistics.action;

import java.util.List;

import com.autologistics.model.Admin;
import com.autologistics.service.AdminService;
import com.autologistics.util.GobalValue;
import com.autologistics.util.PageBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AdminAction extends ActionSupport implements ModelDriven<Admin>{
	Admin admin = new Admin();
	AdminService adminService;
	List<Admin> listadmin;
	private String message;
	
	private PageBean pageBean;
	//默认是1
	private int page=1;
	
	
	/**
	 * 列出所有管理员
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String listAllAdmin(){
		listadmin = adminService.getExceptAll();
		if (listadmin==null){
			message="暂无管理员";
			return SUCCESS;
		}
		pageBean= adminService.queryForOnePage(GobalValue.pageSize, page, listadmin.size());
		listadmin = pageBean.getAdminList();
		return SUCCESS;
	}
	/**
	 * 准添加管理员
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preAddAdmin(){
		return SUCCESS;
	}
	/**
	 * 添加管理员
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String addAdmin(){
		adminService.save(admin);
		return this.listAllAdmin();
	}
	/**
	 * 准备修改管理员信息
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String preEditAdmin(){
		admin=adminService.getById(admin.getId());
		return SUCCESS;
	}
	/**
	 * 修改管理员信息
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String editAdmin(){
		Admin a = adminService.getById(admin.getId());
		admin.setPwd(a.getPwd());
		adminService.update(admin);
		return this.listAllAdmin();
	}
	public String preEditAdminPwd(){
		return SUCCESS;
	}
	/**
	 * 修改管理员密码成功
	 * @author lsyhyp@126.com
	 * @return
	 */
	public String editAdminPwd(){
		Admin a=adminService.getById(admin.getId());
		a.setPwd(admin.getPwd());
		adminService.update(a);
		message="修改密码成功";
		return ERROR;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public List<Admin> getListadmin() {
		return listadmin;
	}
	public void setListadmin(List<Admin> listadmin) {
		this.listadmin = listadmin;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public AdminService getAdminService() {
		return adminService;
	}
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
	@Override
	public Admin getModel() {
		// TODO Auto-generated method stub
		return this.admin;
	}
	
}
